package com.jetinno.syrup2;

import com.jetinno.bean.MachineRespond;
import com.jetinno.utils.JGlobal;
import com.jetinno.utils.LogUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
 * Description : 糖浆机全局状态变量
 */
public class SyrupState {
    private static String TAG="SyrupState";
    private static SyrupState sManager;

    public static SyrupState getInstance() {
        if (sManager == null) {
            synchronized (SyrupState.class) {
                if (sManager == null) {
                    sManager = new SyrupState();
                }
            }
        }
        return sManager;
    }

    private SyrupState() {

    }


    private boolean syrupConnect = false;//糖浆板是否连接
    private boolean isUpdating = false;//糖浆版正在升级中
    private SyrupQueryReceive syrupQueryReceive = new SyrupQueryReceive();

    public boolean hasCupSleeve() {
        return syrupQueryReceive.getHasCupSleeve();
    }

    public boolean isSyrupConnect() {
        return syrupConnect;
    }

    public boolean isUpdating() {
        return isUpdating;
    }

    public boolean isSystemBusy() {
        return syrupQueryReceive.getSystemBusy();
    }

    public void setSystemBusy(boolean systemBusy) {
        syrupQueryReceive.setSystemBusy(systemBusy);
    }

    public void setUpdating(boolean updating) {
        isUpdating = updating;
    }

    public String getFaultCode() {
        return syrupQueryReceive.getFaultCode();
    }

    public int getVersion() {
        return syrupQueryReceive.getVersion();
    }

    public synchronized MachineRespond queryStatus() {
        MachineRespond machineRespond = null;
        try {
            machineRespond = SyrupControlUtils.Companion.queryStatus();
            syrupQueryReceive = SyrupControlUtils.Companion.parseQuery(machineRespond.getHexResult());
        } catch (Exception e) {
            e.printStackTrace();
        }
        syrupConnect = syrupQueryReceive.getVersion() > 0;
        return machineRespond;
    }

    public List<Integer> getOnList() {
        if (JGlobal.isSimulator()) {
            return Arrays.asList(177, 178, 179, 180, 181, 182);
        }
        List<Integer> list = new ArrayList<>();
        ArrayList<Integer> onlineMotorList = syrupQueryReceive.getOnlineMotorList();
        if (onlineMotorList.contains(0)) {
            list.add(177);
        }
        if (onlineMotorList.contains(1)) {
            list.add(178);
        }
        if (onlineMotorList.contains(2)) {
            list.add(179);
        }
        if (onlineMotorList.contains(3)) {
            list.add(180);
        }
        if (onlineMotorList.contains(4)) {
            list.add(181);
        }
        if (onlineMotorList.contains(5)) {
            list.add(182);
        }
        return list;
    }

    ///是否有杯套电机
    public boolean hasCupsleeve() {
        ArrayList<Integer> onlineMotorList = syrupQueryReceive.getOnlineMotorList();
        return onlineMotorList.contains(6);
    }

    private void log(String message) {
        LogUtils.e(TAG,message);
    }

}
